package TextRank;

import Ast.AstUtil;
import Ast.MethodVisitor;
import Count.CountApi;
import dataStructure.ArffFuntion;
import dataStructure.Function;
import org.eclipse.jdt.core.dom.CompilationUnit;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OperateApi {
    /*
    * @param1 path 为源码的路径地址
    * @param2 arffpath 为arff类型的文件路径
    * */
    public void OperateApi(String path,String arffpath) throws IOException {
        //使用ast从源码中获取函数体
        CompilationUnit  comp = AstUtil.getCompilationUnit(path);
        List<Function> list = new ArrayList<>();
        MethodVisitor mv = new MethodVisitor(list);
        comp.accept(mv);
//        String strtest = list.get(2).getFunbady();

        //CountApi用来统计api的个数
        CountApi ca = new CountApi();
        TextRank tr = new TextRank();   //TextRank用来实现关键句摘要

        //获取输出路径
        String outpath = GetOutPath(path);
        try {
            File file = new File(outpath);
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fileWriter);

            //创建匹配对象，使用其中的方法看是否可以更换成api调用模式
            Match mat = new Match();
            //存储arff文件内容
            List<ArffFuntion> arfflist = new ArrayList<>();
            DealArff deal = new DealArff();
            arfflist = deal.DealArff(arffpath);
            //获取输出结果并写入输出文件
            for (Function i : list) {
                //统计函数中使用api的个数
                int temp = ca.CountApi(i.getFunbady());
                if (temp == 0 ){                   //摘要为空
//                    System.out.println(i.getFunname()+"{ }");
                    bw.write(i.getFunname()+"{ }"+"\n");
                }else{
                    List<String> resulttest = new ArrayList<>();
                    resulttest=tr.TextRank(i.getFunbady(),temp);
//                    System.out.println(i.getFunname()+"{");
                    bw.write(i.getFunname()+"{"+"\n");
                    for (String str : resulttest) {
//                        System.out.println(str);
                        //匹配并更换成api调用模式

                        String str2 = mat.Match(i,str,arfflist);
                        if (str.equals(str2)){
                            bw.write(str2+"\n");
                        }else{
                            String space = GetSpace(str);
                            bw.write(space+str2+"\n");
                        }
                    }
//                    System.out.println("}");
                    bw.write("}"+"\n");
                }
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取输出文件路径
    public String GetOutPath(String inputpath){
        String outpath = "";
        outpath = inputpath.replace("resource","resultApi");
        return outpath;
    }

    //获取字符串前面的空格
    public String GetSpace(String str){
        int i = 0;
        String space = "";
        while(str.charAt(i) == ' '){
            i++;
            space += ' ';
        }
        return space;
    }

}

